<template>
  <a-form
    :model="formState"
    ref="formDataRef"
    name="basic"
    :label-col="{ style: 'width:80px' }"
    :wrapper-col="{ style: 'calc(100% - 80px)' }"
    autocomplete="off"
    @finish="onFinish"
    @finishFailed="onFinishFailed"
  >
    <a-form-item label="用户" name="managerId" :rules="[{ required: true, message: '请选择用户!' }]">
      <a-select v-model:value="formState.managerId" placeholder="请输入">
        <a-select-option v-for="item in usertOpts" :key="item.id" :label="item.username" :value="item.id">{{ item.username }}【{{ item.name }}】</a-select-option>
      </a-select>
    </a-form-item>
    <a-form-item :wrapper-col="{ offset: 8, span: 16 }" class="submit-btn-form-item">
      <a-button type="primary" html-type="submit">确定</a-button>
      <a-button @click="resetHandle">取消</a-button>
    </a-form-item>
  </a-form>
</template>

<script lang="ts" setup>
import { toRefs, watch } from "vue";
import InitHook from "./composables/initHook";
import OperationHook from "./composables/operationHook";
const props = withDefaults(
  defineProps<{
    visible: boolean;
    type: number;
    passData: DepartmentPersonnel.ListRows;
  }>(),
  {}
);
const emit = defineEmits<{
  (e: "update:visible", visible: boolean): void;
  (e: "submit"): void;
}>();
const { visible, type, passData } = toRefs(props);
const { formDataRef, formState, usertOpts, getAllUser } = InitHook();
const { onFinish, onFinishFailed, resetHandle, resetFields } = OperationHook({ formDataRef, formState, emit, passData });
const init = () => {
  if (type.value === 2) {
    Object.assign(formState, passData.value);
  }
  getAllUser(passData.value.id);
};
watch(
  () => visible.value,
  (v: boolean) => {
    if (v) {
      init();
    } else {
      resetFields();
      formDataRef.value.clearValidate();
    }
  }
);
init();
</script>
<style lang="stylus" scoped></style>
